package com.gitee.wsl.net.ssdp.description.model.xml

import com.gitee.wsl.logger.Logger
import com.gitee.wsl.logger.info
import com.gitee.wsl.net.ssdp.description.xml.XmlElement
import kotlin.properties.Delegates

/**
 * In device templates, defines the lowest version of the architecture on which the device can
 * be implemented. In actual UPnP devices, defines the architecture on which the device is implemented.
 * @param major - Major version of the UPnP Device Architecture. Shall be 2 for devices implemented
 *                  on a UPnP 2.0 architecture.
 * @param minor - Minor version of the UPnP Device Architecture. Shall be 0 for devices implemented
 *                  on a UPnP 2.0 architecture. Shall accurately reflect the version number of the UPnP Device
 *                  Architecture supported by the device.
 */
data class SpecVersion(
    val major: Int,
    val minor: Int
){
    companion object: Logging{
        /**
         * Facilitates fast and flexible deserialization and conditional validation.
         */
        fun fromXml(xml: XmlElement): SpecVersion {
            var majorI by Delegates.notNull<Int>()
            var minorI by Delegates.notNull<Int>()

            xml.children?.forEach {
                when(it.tag){
                    "major" -> majorI = it.get()
                    "minor" -> minorI = it.get()
                    else -> Logger.info("Unexpected tag ${it.tag}")
                }
            }
            return SpecVersion(majorI, minorI)
        }
    }
}